using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    public partial class FormTemplate : FormBase
    {
        private bool bCreateNewTemplate = false;

        public FormTemplate()
        {
            InitializeComponent();
            PopulateCombo();
        }

        public void CreateNewTemplate()
        {
            this.bCreateNewTemplate = true;

            this.Text = "Create a new template";
        }

        public void EditTemplate(string template_id)
        {
            this.bCreateNewTemplate = false;

            this.Text = "Edit template [" + template_id + "]";

            OpenTemplate(template_id);
        }

        private void OpenTemplate(string template_id)
        {
            //header
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht = doc_template.Get_Template_Header(template_id);

            if (ht.Count > 0)
            {
                LoadDataFromHashToControls(groupBox_HeaderInfo.Controls, ht);
            }

            //field
            System.Collections.Hashtable htFields = new System.Collections.Hashtable();
            htFields = doc_template.Get_Template_Fields(template_id);
            foreach (string key in htFields.Keys)
            {
                string value = htFields[key].ToString();

                ListViewItem aitem = new ListViewItem();
                aitem.Text = key;
                aitem.Tag = key;
                aitem.SubItems.Add(value);

                lsv_TemplateFields.Items.Add(aitem);
            }
        }

        private void PopulateCombo()
        {
            System.Collections.Hashtable ht = FormMain.DocLib_SQLite.GetTableColumnInfo(def.tb_doc);
            foreach (string key in ht.Keys)
            {
                cboField.Items.Add(key);
            }
        }

        private bool ValidateTemplate()
        {
            string err = "";

            if (string.IsNullOrEmpty(txtTemplateID.Text))
                err += "Template ID is required field, please fill in.";

            if (lsv_TemplateFields.Items.Count == 0)
                err += System.Environment.NewLine + "Please add at least one template field.";

            if (string.IsNullOrEmpty(err))
                return true;

            ShowMessage(err);
            return false;
        }

        private void SaveTemplate()
        {
            if (ValidateTemplate())
            {
                wait_start();
                //header
                System.Collections.Hashtable htHeader = SaveDataToHashFromControls(groupBox_HeaderInfo.Controls);
                htHeader["AUTHOR"] = FormMain.DocLib_CurrentUser;

                //field
                System.Collections.Hashtable htField = new System.Collections.Hashtable();
                foreach (ListViewItem aitem in lsv_TemplateFields.Items)
                {
                    string FieldName = aitem.Text;
                    string DefaultValue = aitem.SubItems[1].Text;

                    htField[FieldName] = DefaultValue;
                }

                ShowMessage(doc_template.Save_Template(htHeader, htField));

                wait_end();

                this.Close();
            }            
        }

        private void btnAddField_Click(object sender, EventArgs e)
        {
            string FieldName = cboField.Text.Trim();
            string DefaultValue = txtDefaultValue.Text.Trim();

            //update
            foreach (ListViewItem aitem in lsv_TemplateFields.Items)
            {
                if (aitem.Tag != null && aitem.Tag.ToString() == FieldName)
                {
                    aitem.SubItems[1].Text = DefaultValue;
                    return;
                }
            }

            //add new template fields
            ListViewItem aNewItem = new ListViewItem(FieldName);
            aNewItem.Tag = FieldName;
            aNewItem.SubItems.Add(DefaultValue);

            lsv_TemplateFields.Items.Add(aNewItem);
        }

        private void btnSaveTemplate_Click(object sender, EventArgs e)
        {
            SaveTemplate();
        }

        private void lsv_TemplateFields_Click(object sender, EventArgs e)
        {
            if (lsv_TemplateFields.SelectedItems.Count > 0)
            {
                ListViewItem aitem = lsv_TemplateFields.SelectedItems[0];
                if (aitem.Tag != null && !string.IsNullOrEmpty(aitem.Tag.ToString()))
                {
                    string key = aitem.Tag.ToString();
                    string value = aitem.SubItems[1].Text;

                    cboField.Text = key;
                    txtDefaultValue.Text = value;
                }
            }
        }
    }
}